package com.lin.bank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.bank.config.exception.BusinessException;
import com.lin.bank.config.reponse.ResultCode;
import com.lin.bank.controller.RecordsFeignController;
import com.lin.bank.entity.Account;
import com.lin.bank.entity.TransactionRecord;
import com.lin.bank.feign.RecordsFeign;
import com.lin.bank.mapper.AccountMapper;
import com.lin.bank.service.AccountService;
import com.lin.bank.vo.AccountVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Slf4j
@Component
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {


    @Override
    public AccountVO login(AccountVO accountVO) {
        AccountVO vo = new AccountVO();
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.eq("cardNo", accountVO.getCardNo());
        Account account = this.baseMapper.selectOne(wrapper);
        if (account != null) {
            if (account.getPassword().equals(accountVO.getPassword())) {
                if (account.getStatus() == 0) {
                    throw new BusinessException(ResultCode.USER_ACCOUNT_LOCKED.getCode(),
                            ResultCode.USER_ACCOUNT_LOCKED.getMessage());
                } else
                    vo = accountVO;
            } else {
                throw new BusinessException(ResultCode.USER_CREDENTIALS_ERROR.getCode(),
                        ResultCode.USER_CREDENTIALS_ERROR.getMessage());
            }
        } else {
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),
                    ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }
        return vo;
    }

    @Override
    public Double getBalance(String cardNo) {
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.eq("cardNo", cardNo);
        Double balance = this.baseMapper.selectOne(wrapper).getBalance();
        return balance;
    }

    @Override
    public int updatePassword(String cardNo, String password, String newPwd) {
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.eq("cardNo", cardNo);
        Account account = this.baseMapper.selectOne(wrapper);
        if (!account.getPassword().equals(password)) {
            throw new BusinessException(ResultCode.USER_CREDENTIALS_ERROR.getCode(),
                    ResultCode.USER_CREDENTIALS_ERROR.getMessage());
        }
        account.setPassword(newPwd);
        int i = this.baseMapper.updateById(account);
        return i;
    }



    @Resource
    private RecordsFeign recordsFeign;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int TransferMoney(String cardNo, String cardNo2, Double balance) {

        //得到当前账户对象
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.eq("cardNo", cardNo);
        Account account = this.baseMapper.selectOne(wrapper);
        //判断余额是否大于需转出额度
        if (account.getBalance() < balance) {
            throw new BusinessException(ResultCode.BALANCE_EXCEPTION.getCode(),
                    ResultCode.BALANCE_EXCEPTION.getMessage());
        }
        //给当前账户减钱
        account.setBalance((account.getBalance() - balance));
        this.baseMapper.updateById(account);


        //得到需转入账户对象
        QueryWrapper<Account> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("cardNo", cardNo2);
        Account account2 = this.baseMapper.selectOne(wrapper2);
        if (account2.getStatus() == 0) {
            throw new BusinessException(ResultCode.USER_ACCOUNT_LOCKED.getCode(),
                    ResultCode.USER_ACCOUNT_LOCKED.getMessage());
        } else if (account2 == null) {
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),
                    ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }
        //给需转入账户加钱
        account2.setBalance((account2.getBalance() + balance));
        //提交到数据库更新余额
        this.baseMapper.updateById(account2);
        QueryWrapper<Account> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("cardNo",cardNo);
        Double balance1 = this.baseMapper.selectOne(wrapper1).getBalance();
        QueryWrapper<Account> balanceWrapper2 = new QueryWrapper<>();
        balanceWrapper2.eq("cardNo",cardNo2);
        Double balance2 = this.baseMapper.selectOne(balanceWrapper2).getBalance();

        recordsFeign.sendMsg(cardNo, cardNo2, balance, balance1, balance2);
        return 1;
    }

}
